"""
在一个 m*n 的棋盘的每一格都放有一个礼物，每个礼物都有一定的价值（价值大于 0）。
你可以从棋盘的左上角开始拿格子里的礼物，并每次向右或者向下移动一格、
直到到达棋盘的右下角。给定一个棋盘及其上面的礼物的价值，请计算你最多能拿到多少价值的礼物？
示例 1:
输入:
    [
         [1, 3, 1],
         [1, 5, 1],
         [4, 2, 1]
    ]
输出: 12
解释: 路径 1→3→5→2→1 可以拿到最多价值的礼物
链接：https://leetcode-cn.com/problems/li-wu-de-zui-da-jie-zhi-lcof
"""
from mode import List


class Solution:
    def maxValue(self, grid: List[List[int]]) -> int:
        """
        状态定义：dp[i][j]为到i,j位置所能拿到价值最多的路
        转移方程：dp[i][j] = max(dp[i-1][j], dp[i][j-1]) + nums[i][j]
        初始状态: dp[0][0] = nums[0][0]
        返回值:  dp[-1][-1]
        """
        row, col = len(grid), len(grid[0])
        dp = [[0 for _ in range(col)] for _ in range(row)]
        dp[0][0] = grid[0][0]
        for i in range(row):
            for j in range(col):
                if i == 0 and j == 0:
                    continue
                dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]) + grid[i][j]
        return dp[-1][-1]


class Solution1:
    def maxValue(self, grid: List[List[int]]) -> int:
        """
        状态定义：dp[i][j]为到i,j位置所能拿到的最大价值
        转移方程：dp[i][j] = max(dp[i-1][j],dp[i][j-1]) + grid[i][j]
        初始状态: dp[0][0] = grid[0][0]
        返回值: dp[-1][-1]
        """
        row, col = len(grid), len(grid[0])
        dp = [[0 for _ in range(col)] for _ in range(row)]
        dp[0][0] = grid[0][0]
        for i in range(row):
            for j in range(col):
                if i == 0 and j == 0:
                    continue
                dp[i][j] = max(dp[i-1][j], dp[i][j-1]) + grid[i][j]

        return dp[-1][-1]


if __name__ == "__main__":
    """
    

    """
    A = Solution()
    n = [
        [1, 3, 1],
        [1, 5, 1],
        [1, 5, 1],
        [4, 2, 1]
    ]
    print(A.maxValue(n))

    A = Solution1()
    n = [
        [1, 3, 1],
        [1, 5, 1],
        [1, 5, 1],
        [4, 2, 1]
    ]
    print(A.maxValue(n))
